package com.cgly.medical.service.impl;

import com.cgly.medical.domain.China;
import com.cgly.medical.domain.City;
import com.cgly.medical.entity.CityEntity;
import com.cgly.medical.mapper.ChinaMapper;
import com.cgly.medical.mapper.CityMapper;
import com.cgly.medical.model.CityModel;
import com.cgly.medical.service.CityService;
import com.cgly.medical.util.Msg;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class CityServiceImpl implements CityService {

    @Resource
    CityMapper cityMapper;

    @Resource
    ChinaMapper chinaMapper;

    //获取所有的城市信息
    @Override
    public Msg getAll(Integer page,Integer limit) {
        List<City> list1 = cityMapper.getAll();
        PageHelper.startPage(page,limit);
        List<City> cities = cityMapper.getAll();

        List<CityModel> list = new ArrayList<>();
        for (City city : cities) {

            //临时变量
            CityModel temp = new CityModel();

            //得到cityNumber
            Integer cityNumber = city.getCityNumber();

            //设置到temp里面
            temp.setCityNumber(cityNumber);

            //得到的是区(China表里面查询)第一次查到的是一个区级地名
            China china = chinaMapper.selectById(cityNumber);

            //名称设置到temp里面
            temp.setCity(china.getName());

            //得到的就是省
            China china1 = chinaMapper.selectById(china.getParentId());
            temp.setProvince(china1.getName());

            list.add(temp);
        }

        PageInfo<CityModel> cityModelList=new PageInfo<>(list,5);
        System.out.println(cityModelList.getSize()+"===========");
        cityModelList.setTotal(list1.size());
        return Msg.success().data("cityList", cityModelList);
    }

    //根据City_number删除城市信息
    @Override
    public Msg deleteCityByNumber(Integer id) {
        int row = cityMapper.deleteCityByNumber(id);
        if (row == 0) {
            return Msg.fail().mess("城市信息删除有误");
        } else {
            return Msg.success().mess("城市信息删除成功");
        }

    }

}
